草庐IT

java - 构造矩形

全部标签

Javascript在数组中使用构造函数

这个问题在这里已经有了答案:LoopthroughanarrayinJavaScript(46个答案)关闭7年前。我有这样的代码,然后我对如何循环数组族感到困惑将每个成员打印在person下。functionPerson(name,age){this.name=name;this.age=age;}varfamily=[];family[0]=newPerson("alice",40);family[1]=newPerson("bob",42);family[2]=newPerson("michelle",8);family[3]=newPerson("timmy",6);

javascript - 可以从函数原型(prototype)访问私有(private)构造函数范围的变量吗?

根据我对javascript的理解,原型(prototype)方法不能访问构造函数范围内私有(private)的变量,varFoo=function(){varmyprivate='Iamprivate';this.mypublic='Iampublic';}Foo.prototype={alertPublic:function(){alert(this.mypublic);}//willworkalertPrivate:function(){alert(myprivate);}//won'twork}这很有道理,但有没有什么安全且好的方法可以解决这个问题?由于使用原型(prototy

javascript - Canvas :矩形——对齐网格/对齐对象

我设法通过以下方式操作Fabric.js以向网格功能添加捕捉和缩放:vargrid=100;//SnaptoGridcanvas.on('object:moving',function(options){options.target.set({left:Math.round(options.target.left/grid)*grid,top:Math.round(options.target.top/grid)*grid});});canvas.on('object:scaling',function(options){options.target.set({left:Math.ro

javascript - 从构造函数中将方法附加到原型(prototype)

这是描述JavaScript中“类”或构造函数的教科书标准方法,直接来自JavaScript权威指南:functionRectangle(w,h){this.width=w;this.height=h;}Rectangle.prototype.area=function(){returnthis.width*this.height;};我不喜欢这里的悬空原型(prototype)操作,所以我试图想办法将area的函数定义封装在构造函数中。我想到了这个,但我不期望它能工作:functionRectangle(w,h){this.width=w;this.height=h;this.con

javascript - 为什么在 javascript 中列出类的实际构造函数很重要

我正在阅读javascriptgardenhttp://bonsaiden.github.com/JavaScript-Garden/关于javascript中的原型(prototype)及其示例之一是这样的:functionFoo(){this.value=42;}Foo.prototype={method:function(){}};functionBar(){}//SetBar'sprototypetoanewinstanceofFooBar.prototype=newFoo();Bar.prototype.foo='HelloWorld';//MakesuretolistBar

javascript - 什么时候在 React 组件中使用构造函数?

我总是编写React代码,尤其是在ES6类中。但我的问题是,我们什么时候在ReactComponents中使用constructor(props)?constructor(props)行是否与组件及其props的渲染有关? 最佳答案 已接受的答案不正确(可能只是误用了“渲染”一词)。正如我在我的评论中解释的那样React组件的构造函数在第一次安装或实例化组件时执行一次。它永远不会在后续渲染中再次调用。通常构造函数用于设置组件的内部状态,例如:constructor(){super()this.state={//internalsta

javascript - 使用 React 时,在构造函数中使用粗箭头函数还是绑定(bind)函数更可取?

在创建React类时,哪个更可取?exportdefaultclassFooextendsReact.Component{constructor(props){super(props)this.doSomething=this.doSomething.bind(this)}doSomething(){...}}或exportdefaultclassFooextendsReact.Component{doSomething=()=>{...}}我的一个同事认为后者会导致内存问题,因为babel转译代码以在闭包内捕获this,而该引用将导致实例不被GC清理。对此有什么想法吗?

Javascript 继承——在构造函数中声明的对象在实例之间共享?

我在没有Prototype/jQuery的情况下用JavaScript进行面向对象的编程(我使用jQuery做其他事情)。到目前为止它一直运行良好,但我遇到了继承问题。基本上,当我在构造函数中声明对象时,它们在实例之间共享。下面是一些示例代码:A=function(){this.y=newArray();}A.prototype.doStuff=function(n){this.y.push(n);}B=function(){}B.prototype=newA();varb1=newB();varb2=newB();b1.doStuff(100);b2.doStuff(200);con

javascript - ReSharper 对构造函数的 JavaScript 命名约定的警告

在JavaScript中,我喜欢构造函数的PascalCase命名约定和其他函数的camelCase命名约定。看起来ReSharper已针对这些设置进行了配置。但是,对于这样的代码:functionThing(a,b){return{prop1:a,prop2:b};}varthing=newThing(2,6);...我收到此警告:Name'Thing'doesnotnotmatchrule'LocalFunction'.Suggestednameis'thing'.如果我将Thing更改为:functionThing(a,b){this.prop1=a;this.prop2=b;}

javascript - 如何让构造函数继承自 Javascript 中的构造函数?

所以我正在学习Javascript及其所有原型(prototype)优点,但我对以下内容感到困惑:说我有这个varAnimal=function(a,b,c,d,e,f,g,h,i,j,k,l,m,n){this.a=a;this.b=b;//...etc...};varx=newAnimal(1,2,3....);现在如何创建一个继承自Animal构造函数的Cat构造函数,这样我就不必再次键入超长参数?换句话说,我不想这样做:varCat=function(a,b,c,d,e,f,g,h,i,j,k,l,m,n){this.a=a;this.b=b;//...etc...};//in